package com.atqh.logistics.service.impl;


import com.atqh.logistics.domin.Manager;
import com.atqh.logistics.mapper.ManagerMapper;
import com.atqh.logistics.mapper.MissionMapper;
import com.atqh.logistics.utils.ErrorCode;
import com.atqh.logistics.utils.Result;
import com.atqh.logistics.utils.Vo.params.ManagerRegisParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atqh.logistics.service.ManagerService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 *
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager>
        implements ManagerService {
    private static final String salt = "lum!@#";
    @Autowired
    private  ManagerMapper managerMapper;
    @Autowired
    private  MissionMapper missionMapper;


    @Override
    public Result login(String account, String password, HttpServletRequest req) {

        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        String psw = DigestUtils.md5Hex(password + salt);
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getAccount, account).eq(Manager::getPassword, psw).last("limit 1");
        Manager manager = managerMapper.selectOne(queryWrapper);
        if (manager == null) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        } else if (manager != null) {
            req.getSession().setAttribute("manager",manager);
        }
        return Result.success(manager);
    }

    @Override
    public Result logout(HttpServletRequest req) {

        Manager manager = (Manager) req.getSession().getAttribute("manager");
        if (manager != null) req.getSession().invalidate();
        return Result.success(null);
    }

    @Override
    public Result register(ManagerRegisParam managerRegisParam) {
        String account = managerRegisParam.getAccount();
        String password = managerRegisParam.getPassword();
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            return Result.Fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }

        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getAccount, account);
        Manager custom = managerMapper.selectOne(queryWrapper);

        if (custom!=null) {
            return Result.Fail(ErrorCode.ACCOUNT_EXIST.getCode(), "账号已经被用了欧");
        }
        Manager custom1 = new Manager();
        custom1.setAccount(account).setName(managerRegisParam.getName());
        custom1.setPassword(DigestUtils.md5Hex(password + salt));
        managerMapper.insert(custom1);
        return Result.success(null);
    }
}




